//
//  StoreMapShowViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/6/2.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "StoreMapShowViewController.h"
#import "CustomPaopaoView.h"
#import "ABLocation.h"
#import "CPMapNavigation.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
@interface StoreMapShowViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
@property(nonatomic,weak) BMKMapView *mapView;//地图

@property(nonatomic,strong) BMKLocationService *locService;//定位
@end

@implementation StoreMapShowViewController
-(void)rightAction:(UIButton *)sender
{
    CLLocation *originLocaiton  = [ABLocation sharedLocation].lastLocation;
    if ([ABLocation isCanLocation] == NO || originLocaiton == nil) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"开启定位或者等待定位成功"];
        return;
    }
    
    [[CPMapNavigation sharedMapNavigation] clickGotoMapWithDestinationName:self.storeItem.storeName DestinationCoordinate:CLLocationCoordinate2DMake(self.storeItem.latitude.floatValue, self.storeItem.longitude.floatValue) originCoordinate: originLocaiton.coordinate];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavigationItems];
    mNavitationTitle(@"导航");
     [self setupMapView];
     [self.locService startUserLocationService];
    [self addRightBarButtonItemWithTitle:@"导航" action:@selector(rightAction:)];
    // Do any additional setup after loading the view.
    
    
}
- (void)setupMapView{
    BMKMapView *mapView = [[BMKMapView alloc]init];
    [self.view addSubview:mapView];
    [mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(getRectNavAndStatusHight);
        make.bottom.equalTo(self.view);
    }];
    self.mapView = mapView;
    
    UIButton *button = [[UIButton alloc]init];
    [button setImage:[UIImage imageNamed:@"gpers_icon"] forState:UIControlStateNormal];
    [mapView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(mapView);
    }];
    
    self.mapView.showsUserLocation = YES;//显示定位图层
    self.mapView.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态为普通定位模式
    self.mapView.zoomLevel = 16;//地图显示的级别
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(self.storeItem.latitude.floatValue, self.storeItem.longitude.floatValue);
    [self.mapView setCenterCoordinate:pt animated:YES];
    
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    annotation.coordinate = CLLocationCoordinate2DMake(self.storeItem.latitude.floatValue, self.storeItem.longitude.floatValue);
    //设置标注的标题
    annotation.title = self.storeItem.storeName;
    //副标题
    annotation.subtitle = self.storeItem.detailAddress;
    [_mapView addAnnotation:annotation];
    
}
#pragma mark - BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        BMKPinAnnotationView*annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.pinColor = BMKPinAnnotationColorRed;
        annotationView.canShowCallout= YES;      //设置气泡可以弹出，默认为NO
        [annotationView setSelected:YES animated:YES];
        annotationView.animatesDrop=YES;         //设置标注动画显示，默认为NO
        annotationView.draggable = YES;          //设置标注可以拖动，默认为NO
        return annotationView;
    }
    return nil;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.mapView viewWillAppear];
    self.mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil; // 不用时，置nil
   
}
#pragma mark - lazyLoad
- (BMKLocationService *)locService{
    if (_locService == nil) {
        //初始化BMKLocationService
        _locService = [[BMKLocationService alloc]init];
        _locService.delegate = self;
    }
    return _locService;
}
/**
 *在将要启动定位时，会调用此函数
 */
- (void)willStartLocatingUser
{
    
}
/**
 *在停止定位后，会调用此函数
 */
- (void)didStopLocatingUser
{
    
}
/**
 *用户方向更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [self.mapView updateLocationData:userLocation]; //更新地图上的位置
    
}
/**
 *定位失败后，会调用此函数
 *@param error 错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
